package org.lex.net.proxy.ui;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.lex.net.proxy.ProxyServer;
import org.lex.swing.widgets.EnhancedDialog;


public class ProxyServerDialog extends EnhancedDialog {
	private ProxyServerPane proxyServerPane = null;
	private ProxyServer ps = null;

	public static void main(String[] args) {
		System.out.println(ProxyServerDialog.showAddDialog(null));
	}

	public static ProxyServer showAddDialog(Window root) {
		return ProxyServerDialog.showDialog(root, "Add", null);
	}

	public static ProxyServer showEditDialog(Window root, ProxyServer ps) {
		return ProxyServerDialog.showDialog(root, "Edit", ps);
	}

	public static ProxyServer showDialog(Window root, String title, ProxyServer ps) {
		ProxyServerDialog d = new ProxyServerDialog(root);
		d.proxyServerPane.setProxyServer(ps);
		d.setTitle(title);
		d.setModal(true);
		d.setLocationRelativeTo(root);
		d.setVisible(true);
		return d.ps;
	}

	public ProxyServerDialog(Window root) {
		super(root);
		this.initialize();
		this.hookActionListeners();
	}

	public void cancel() {
		ps = null;
		this.dispose();
	}

	public void ok() {
		ps = this.getProxyServerPane().getProxyServer();
		this.dispose();
	}

	private void hookActionListeners() {
		this.setDefaultButton(proxyServerPane.getOkButton());
		proxyServerPane.getCancelButton().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				cancel();
			}
		});
		proxyServerPane.getOkButton().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ok();
			}
		});
	}

	private void initialize() {
		this.setSize(400, 275);
		this.setContentPane(this.getProxyServerPane());
	}

	private ProxyServerPane getProxyServerPane() {
		if (null == proxyServerPane)
			proxyServerPane = new ProxyServerPane();
		return proxyServerPane;
	}
}
